package com.lsnu.wzy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lsnu.wzy.po.Department;
import com.lsnu.wzy.po.Doctor;
import com.lsnu.wzy.service.DepartMentService;

@RestController
@RequestMapping("/departmentOption")
public class DepartMentController {
	
	@Autowired
	private  DepartMentService departMentService;

	//按照dpID查找科室
	@RequestMapping("/findById/{dpID}")
	public ModelAndView FindById(String dpID)
	{
		List<Department> list = new ArrayList<Department>();
		Department departMent= departMentService.findDepartMent(dpID);
		list.add(departMent);
		ModelAndView mav=new ModelAndView("/jsp/pages/departments-list.jsp");
		mav.addObject("departMent", list);
		mav.addObject("dpID",dpID);
		return mav;		
	}
	
	//管理员点击添加事件
	@RequestMapping("/beforeADD")
	public ModelAndView beforeADD()
	{
		Department departMent=departMentService.findLast();
		String dpID=departMent.getDpID();
		char[] arr=dpID.toCharArray();
		StringBuilder builder = new StringBuilder();
		  for (int i = 0; i < arr.length; i++) {
//			      	如果字符的ACSLL码在48-57的范围内那肯定就是整数了
				if(arr[i] >= 48 && arr[i] <= 57) {
					builder.append(arr[i]);
				}
			}
		Integer integer=(Integer.parseInt(builder.toString()))+1;
		String dpString="dp00"+integer;
		ModelAndView mav=new ModelAndView("/jsp/pages/addDepartment.jsp");
		mav.addObject("dpID",dpString);
		return mav;
	}
	//添加科室
	@RequestMapping("/addDepartment")
	public void ADDGoods(HttpServletRequest request,HttpServletResponse response,Department departMent) throws ServletException, IOException 
	{
		departMentService.addDepartMent(departMent);
        request.getRequestDispatcher("/departmentOption/lsDepartment").forward(request,response);
	}
	
	//删除科室
	@RequestMapping("/delDepartment/{dpID}")
	  public void DELGoods(HttpServletRequest request,HttpServletResponse response,@PathVariable String dpID) throws ServletException, IOException 
	{
		System.out.println("要删除的科室dpID为"+dpID);
		departMentService.delDepartMent(dpID);
	  request.getRequestDispatcher("/departmentOption/lsDepartment").forward(request,response); 
	}
	
	//编辑前的搜索
	@RequestMapping("/editfind/{dpID}")
	public ModelAndView FindById2(@PathVariable String dpID)
	{
		List<Department> list = new ArrayList<Department>();
		Department departMent= departMentService.findDepartMent(dpID);
		list.add(departMent);
		ModelAndView mav=new ModelAndView("/jsp/pages/editDepartment.jsp");
		mav.addObject("departMent", list);
		mav.addObject("dpID",dpID);
		return mav;		
	}
	
	//编辑科室
	@RequestMapping("/editDepartment/{id}")
	  public void EditGoods(HttpServletRequest request,HttpServletResponse response, Department departMent) throws ServletException, IOException 
	{
		
		departMentService.editDepartMent(departMent);
	    request.getRequestDispatcher("/departmentOption/lsDepartment").forward(request,response); 
	}
	 
	//科室列表
	@RequestMapping("/lsDepartment")
	public ModelAndView LSDepartment()
	{
		ModelAndView mav=new ModelAndView();
		List<Department> departMentlist= departMentService.LSDepartMent();
		
		/*
		 * List<String> doctorlist=null; List<String> list=null;
		 * 
		 * for(int i=0;i<departMentlist.size();i++){ Department departMent=
		 * departMentService.findDepartMent("dp00"+(i+1));
		 * list=departMentService.LSDeDoctor(departMent.getDpName());
		 * System.out.println(list); }
		 */
		//for(String str:list) { doctorlist.add(str); }
		
		//mav.addObject("list",doctorlist);
		mav.addObject("departMent",departMentlist);
		
		mav.setViewName("/jsp/pages/departments-list.jsp");
		return mav;		
	}
	
	//按照dpName查找科室
	@RequestMapping("/findByName/{dpName}")
	public ModelAndView FindDepartment(String dpName)
	{
		List<Department> list = new ArrayList<Department>();
		Department departMent= departMentService.findByName(dpName);
		list.add(departMent);
		ModelAndView mav=new ModelAndView("/jsp/index2.jsp");
		mav.addObject("departMent", list);
		mav.addObject("dpName",dpName);
		return mav;		
	}
	
	//科室列表
	@RequestMapping("/lsDepartment2")
	public ModelAndView LSDepartment2()
	{
		ModelAndView mav=new ModelAndView();
		List<Department> departMentlist= departMentService.LSDepartMent();
	
		mav.addObject("departMent",departMentlist);
		mav.setViewName("/jsp/index2.jsp");
		return mav;		
	}

}
